package com.monitoring.domain.service;

import com.monitoring.domain.entity.Device;
import com.monitoring.domain.entity.AirConditioner;
import com.monitoring.domain.entity.ElectricMeter;
import com.monitoring.domain.repository.DeviceRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeviceService {
    private final DeviceRepository deviceRepository;
    
    public DeviceService(DeviceRepository deviceRepository) {
        this.deviceRepository = deviceRepository;
    }
    
    // 创建空调
    public AirConditioner createAirConditioner(String name, String location) {
        AirConditioner ac = new AirConditioner();
        ac.setName(name);
        ac.setLocation(location);
        ac.setType("AIR_CONDITIONER");
        ac.setActive(true);
        ac.setStatus("正常");
        ac.setTemperature(25); // 默认温度
        ac.setMode("制冷"); // 默认模式
        ac.setFanSpeed(2); // 默认风速
        
        return deviceRepository.save(ac);
    }
    
    // 创建电表
    public ElectricMeter createElectricMeter(String name, String location) {
        ElectricMeter meter = new ElectricMeter();
        meter.setName(name);
        meter.setLocation(location);
        meter.setType("ELECTRIC_METER");
        meter.setActive(true);
        meter.setStatus("正常");
        meter.setCurrentReading(0.0);
        meter.setTotalConsumption(0.0);
        
        return deviceRepository.save(meter);
    }
    
    // 根据ID查找设备
    public Optional<Device> getDeviceById(Long id) {
        return deviceRepository.findById(id);
    }
    
    // 获取所有设备
    public List<Device> getAllDevices() {
        return deviceRepository.findAll();
    }
    
    // 根据类型查找设备
    public List<Device> getDevicesByType(String type) {
        return deviceRepository.findByType(type);
    }
    
    // 根据位置查找设备
    public List<Device> getDevicesByLocation(String location) {
        return deviceRepository.findByLocation(location);
    }
    
    // 打开设备
    public Device turnOnDevice(Long id) {
        Optional<Device> optionalDevice = deviceRepository.findById(id);
        if (optionalDevice.isPresent()) {
            Device device = optionalDevice.get();
            device.turnOn();
            return deviceRepository.save(device);
        }
        throw new IllegalArgumentException("设备不存在");
    }
    
    // 关闭设备
    public Device turnOffDevice(Long id) {
        Optional<Device> optionalDevice = deviceRepository.findById(id);
        if (optionalDevice.isPresent()) {
            Device device = optionalDevice.get();
            device.turnOff();
            return deviceRepository.save(device);
        }
        throw new IllegalArgumentException("设备不存在");
    }
    
    // 激活设备
    public Device activateDevice(Long id) {
        Optional<Device> optionalDevice = deviceRepository.findById(id);
        if (optionalDevice.isPresent()) {
            Device device = optionalDevice.get();
            device.activate();
            return deviceRepository.save(device);
        }
        throw new IllegalArgumentException("设备不存在");
    }
    
    // 停用设备
    public Device deactivateDevice(Long id) {
        Optional<Device> optionalDevice = deviceRepository.findById(id);
        if (optionalDevice.isPresent()) {
            Device device = optionalDevice.get();
            device.deactivate();
            return deviceRepository.save(device);
        }
        throw new IllegalArgumentException("设备不存在");
    }
    
    // 设备维护
    public Device putDeviceInMaintenance(Long id) {
        Optional<Device> optionalDevice = deviceRepository.findById(id);
        if (optionalDevice.isPresent()) {
            Device device = optionalDevice.get();
            device.putInMaintenance();
            return deviceRepository.save(device);
        }
        throw new IllegalArgumentException("设备不存在");
    }
    
    // 恢复设备运行
    public Device resumeDeviceOperation(Long id) {
        Optional<Device> optionalDevice = deviceRepository.findById(id);
        if (optionalDevice.isPresent()) {
            Device device = optionalDevice.get();
            device.resumeOperation();
            return deviceRepository.save(device);
        }
        throw new IllegalArgumentException("设备不存在");
    }
    
    // 删除设备
    public void deleteDevice(Long id) {
        if (deviceRepository.existsById(id)) {
            deviceRepository.deleteById(id);
        } else {
            throw new IllegalArgumentException("设备不存在");
        }
    }
    
    // 获取活跃设备
    public List<Device> getActiveDevices() {
        return deviceRepository.findActiveDevices();
    }
}